#include "game.h"

Game::Game(wxFrame *parent, int *args) :
		wxGLCanvas(parent, wxID_ANY,  wxDefaultPosition, wxDefaultSize, 0, wxT("GLCanvas"),  args) {
	x = 0.0;
	y = 0.0;

	stopWatch.Start();
}

void Game::OnUpdate(wxIdleEvent& event) {
	stopWatch.Start();

	Refresh();

	if (wxGetKeyState(WXK_ESCAPE))
		exit(0);

	x += Keys[WXK_RIGHT] - Keys[WXK_LEFT];
	y += Keys[WXK_DOWN] - Keys[WXK_UP];

	if (stopWatch.Time() < 1000 / 60)
		wxMilliSleep(1000 / 60 - stopWatch.Time());
}
void Game::OnPaint(wxPaintEvent& event) {
	static bool first = true;

	SetCurrent();
	wxPaintDC(this);

	if (first) {
		first = false;
		glViewport(0, 0, 320, 240);
		glEnable(GL_TEXTURE_2D);
		glEnable(GL_COLOR_MATERIAL);
		glEnable(GL_BLEND);
		glDisable(GL_DEPTH_TEST);
		glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);

		glMatrixMode(GL_PROJECTION);
		glLoadIdentity();
		glOrtho(0.0, 320.0, 240.0, 0.0, -1.0, 1.0);
		glMatrixMode(GL_MODELVIEW);
		glLoadIdentity();

		glClearColor(0.5, 0.5, 0.5, 1.0);
		glClear(GL_COLOR_BUFFER_BIT);

		test = new Image(wxT("test.bmp"));
	}

	glClearColor(0.5, 0.5, 0.5, 1.0);
	glClear(GL_COLOR_BUFFER_BIT);

	test->Draw(x, y);

	SwapBuffers();
}
void Game::Empty(wxEraseEvent& event) {}
void Game::OnKeyDown(wxKeyEvent& event) {
	Keys[event.GetKeyCode()] = true;
}
void Game::OnKeyUp(wxKeyEvent& event) {
	Keys[event.GetKeyCode()] = false;
}
